cc.Class({
    extends: cc.Component,

    properties: {
        operationBtns: [cc.Node],
        mjprefab: cc.Prefab,
        chiLayer: cc.Node,
        _chiList: [],
        _chiNodes: [],
        _chiTarget: 0,
        _operation: 0,
        operation: {
            get() { return this._operation; },
            set(n) {
                if (n == 0) {
                    this.node.active = false;
                    return;
                }
                this.chiLayer.active = false;
                this.node.active = true;
                this._operation = n;
                let beginX = 310;
                let intervalX = -135;
                let index = 0;
                for (let i = this.operationBtns.length - 1; i >= 0; i--) {
                    let a1 = Math.pow(10, i + 1);
                    let a2 = Math.pow(10, i);
                    if (Math.floor(n % a1 / a2) == 1) {
                        this.operationBtns[i].active = true;
                        this.operationBtns[i].x = beginX + index * intervalX;
                        index++
                    } else {
                        this.operationBtns[i].active = false;
                    }
                }
            },
        },
    },

    start() {

    },

    clearChiMj() {
        for (let i = 0; i < this._chiNodes.length; i++) {
            const node = this._chiNodes[i];
            node.destroy();
        }
        this._chiNodes = [];
    },

    showChiList() {
        // this._chiList = list;
        if (this._chiList.length == 0 || this._chiTarget == 0) {
            return;
        }
        this.clearChiMj();

        this.chiLayer.active = true;
        this.chiLayer.width = this._chiList.length * 3 * 83 + this._chiList.length * 10 + 20;
        let x = -this.chiLayer.width / 2 + 10 + 42;
        let self = this;
        for (let i = 0; i < this._chiList.length; i++) {
            let zuhe = this._chiList[i];
            let zuhecopy = zuhe.slice(0);
            zuhecopy.push(this._chiTarget);
            zuhecopy.sort((a, b) => {
                return a - b;
            });
            for (let n = 0; n < zuhecopy.length; n++) {
                let mj = cc.instantiate(this.mjprefab);
                let reslogic = mj.getComponent('MajiangRes');
                reslogic.majiang = zuhecopy[n];
                reslogic.chizu = zuhe;
                mj.parent = this.chiLayer;
                mj.x = x;
                mj.y = 0;
                x += mj.width;
                reslogic.canSend = true;
                reslogic.selectFunc = () => {
                    self.chiBtnCallback(zuhe);
                }
                this._chiNodes.push(mj);
            }
            x += 10;
        }
    },

    chiBtnCallback(zuhe) {
        let senddata = {
            userid: cc.ll.player.userid,
            opertype: 'chi',
            operparam: zuhe,
        }
        cc.ll.net.send('mj_oper', senddata);
        this.chiLayer.active = false;
        this.operation = 0;
    },

    // update (dt) {},
    onOperBtnClicked(e, d) {
        if (cc.ll.net.hasClose == false) {
            if (d == 'chi') {
                if (this._chiList.length == 1) {
                    this.chiBtnCallback(this._chiList[0]);
                } else {
                    this.showChiList();
                }
            } else if (d == 'cancel') {
                cc.ll.net.send('mj_oper_cancel');
                this.operation = 0;
            } else {
                let senddata = {
                    userid: cc.ll.player.userid,
                    opertype: d,
                }
                cc.ll.net.send('mj_oper', senddata);
                this.operation = 0;
            }
        }
    },
});
